import * as CircularJSON from 'circular-json'

class LocalCache {
  // 使用 circular-json 安全序列化
  private safeStringify(value: any): string {
    return CircularJSON.stringify(value)
  }

  setCache(key: string, value: any) {
    window.localStorage.setItem(key, this.safeStringify(value))
  }

  getCache(key: string) {
    // 使用 circular-json 反序列化
    const value = window.localStorage.getItem(key)
    if (value) {
      return CircularJSON.parse(value)
    }
  }

  deleteCache(key: string) {
    window.localStorage.removeItem(key)
  }

  clearCache() {
    window.localStorage.clear()
  }
}

export default new LocalCache()
